<?php 
$contractList = ContractUtil::getContractListByLocation($dispatch->custno,$dispatch->locno);
$statusList = ContractStatus::model()->findAll(array(
	'condition'=>'inactive = 0',
	'order'=>'sort'
	));
?>
<style>
.equipmentRow{
	background-color: papayawhip;
}
</style>
<script>
$(document).ready(function(){
	$('.equipmentRow').hide();
	$('.contractRow').click(function(){
		if($(this).data('showOrHide')==null || $(this).data('showOrHide') == 'hide'){
			$(this).html('&nbsp;-&nbsp;');
			$(this).parent().parent().next().children('.equipmentRow').show();
			$(this).data('showOrHide','show')
		}else{
			$(this).html('&nbsp;+&nbsp;');
			$(this).parent().parent().next().children('.equipmentRow').hide();
			$(this).data('showOrHide','hide')
		}
	});
});

function showEquipmentByType(id){
	$('.equipmentRow').hide();
    $('.contractRow').each(function(){
        $(this).html('&nbsp;+&nbsp;');        
        $(this).data('showOrHide','hide')
    });
	$('#list-id-'+id+' .contractRow').each(function(){        
		if($(this).data('showOrHide')==null || $(this).data('showOrHide') == 'hide'){
			$(this).html('&nbsp;-&nbsp;');
			$(this).parent().parent().next().children('.equipmentRow').show();
			$(this).data('showOrHide','show')
		}else{
			$(this).html('&nbsp;+&nbsp;');
			$(this).parent().parent().next().children('.equipmentRow').hide();
			$(this).data('showOrHide','hide')
		}
	});
}
</script>
<br/>
	<?php foreach($statusList as $type):?>
		<?php
		echo CHtml::link("<b>$type->description</b>",'javascript:void(0)',array('onclick'=>"showEquipmentByType($type->id);"));
        $table = new CI_Table();
        $table->set_template(array(
            'table_open'=>'<table width="800px" border="1">'
        ));
        $table->set_heading(
            'Type',
            'Contract No',
            'Start date',
            'End date'            
        );

		?>
		<div id="list-id-<?=$type->id?>">
        	<?php foreach($contractList as $contract):?>
        		<?php if($contract->status==$type->id):?>
        			<?php 
        			$contractLink = CHtml::link(                        
        				$contract->contractid,
        				array('/contracts/update','contractid'=>$contract->contractid),
        				array(
        					'target'=>'_blank'
        				)
        			);
        			$table->add_row(
                        '<span class="contractRow">&nbsp;+&nbsp;</span>'.ContractType::getText($contract->contractype),
        				$contractLink,
        				$contract->contractstartdate,
        				$contract->contractenddate
        			);

        			$equipmentTable = new CI_Table;
        			$equipmentTable->set_template(array(
        				'table_open'=>'<table width="100%" border=1>'
        			));
                    if($contract->contractype == 4){
                        $equipmentTable->set_heading(array(
                            array(
                                'data'=>'',
                                'style'=>'width:50px'
                            ),
                            'QSI Equip',
                            'Description',
                            'S/N',
                            'Manufacturer',
                            'Model',
                            'Software Maintenance Options'
                        ));
                        if(isset($contract->equipments[0])){
                            $equipment = $contract->equipments[0];
                            $softwareOptionsText = '';
                            if(isset($contract->softwareOptions[0])){
                                $softwareOptionsText = 
                                'ID 1:'. $contract->softwareOptions[0]->software_product_id_1.
                                'ID 2:'. $contract->softwareOptions[0]->software_id_2;

                            }

                            if($equipment->original == null){
                                $equipmentTable->add_row(array(
                                    array(
                                        'data'=>'',
                                        'style'=>'width:50px;'
                                    ),
                                    array(
                                        'data'=>"Original Equipment id $equipment->equipmentid belong to contract $equipment->contractid is missing database.",
                                        'colspan'=>6
                                    )
                                ));
                            }else{
                               $equipmentTable->add_row(array(
                                    array(
                                        'data'=>'',
                                        'style'=>'width:50px;'
                                    ),
                                    CHtml::link(
                                        $equipment->equipmentid,
                                        array(
                                            '/equipment/update',
                                            'id'=>$equipment->equipmentid),array('target'=>'_blank')),
                                    $equipment->original->description,
                                    $equipment->original->serial,
                                    $equipment->original->mfg,
                                    $equipment->original->model,
                                    $softwareOptionsText
                                ));   
                            }
                              
                        }
                        
                    }else{
                        $equipmentTable->set_heading(array(
                            array(
                                'data'=>'',
                                'style'=>'width:50px;'
                            ),
                            'QSI Equip',
                            'Description',
                            'Plan'
                        ));

                        foreach($contract->equipments as $equipment){

                            if($equipment->original ==null){
                                $equipmentTable->add_row(
                                    array(
                                        array(
                                            'data'=>'',
                                            'style'=>'width:50px;'
                                        ),
                                        array(
                                            'data'=>"Original Equipment id $equipment->equipmentid belong to contract $equipment->contractid is missing database.",
                                            'colspan'=>3
                                        )
                                        
                                    )
                                );
                            }else{
                                $equipmentTable->add_row(
                                    array(
                                        array(
                                            'data'=>'',
                                            'style'=>'width:50px;'
                                        ),
                                        CHtml::link($equipment->equipmentid,array('/equipment/update','id'=>$equipment->equipmentid),array('target'=>'_blank')),
                                        $equipment->original->description,
                                        $equipment->getPlanTypeText(),
                                        
                                    )
                                );
                            }
                            
                        }
                    }
        			
                    
        			$table->add_row(
        				array(
        					'data'=>$equipmentTable->generate(),
        					'class'=>'equipmentRow',
        					'colspan'=>4)
        			);
        			?>
        		<?php endif;?>
        	<?php endforeach; ?>
        	<?php echo $table->generate(); ?>
        	<br/>
        </div>
	<?php endforeach; ?>
